package com.yvan.serverless;

import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(name = "serverless.compile.auto")
@EnableConfigurationProperties({ServerLessProperties.class})
public class ScriptCompileAutoConfiguration {

    @Autowired
    public ServerLessProperties properties;

    @Bean("autoCompileClient")
    public ScriptCompiler autoCompileClient() {
        if (!Strings.isNullOrEmpty(properties.getCompile().getClient())) {
            return new ScriptCompiler(properties.getCompile().getClient(), false);
        }
        return null;
    }

    @Bean("autoCompileServer")
    public ScriptCompiler autoCompileServer() {
        if (!Strings.isNullOrEmpty(properties.getCompile().getServer())) {
            return new ScriptCompiler(
                    properties.getCompile().getServer(),
                    properties.getCompile().getReloadEngine()
            );
        }
        return null;
    }

}
